﻿#include "Score.h"
#include"Sprite.h"
#include<Qdebug>
Score::Score(const QRect& rect, qreal xRadius, qreal yRadius):
    m_rect(rect),
    m_xRadius(xRadius),
    m_yRadius(yRadius)
{

}

Score::~Score()
{
}

void Score::update()
{
}

void Score::draw(QPainter* painter)
{
    if (m_player == nullptr)
        return;
    //暂时的简单显示分数
    
    
    painter->setPen(m_RectPen);
    painter->drawRoundedRect(m_rect, m_xRadius, m_yRadius);

    m_NumFont.setPixelSize(32);
    painter->setPen(m_NumPen);
    painter->setFont(m_NumFont);

    QString Num = QString::number(m_player->getTotalScore());
    int w = painter->fontMetrics().horizontalAdvance(Num);
    int n = (m_rect.width() - w) / painter->fontMetrics().horizontalAdvance("0");
    QString str;
    for (size_t i = 0; i < n; i++)
    {
        str += "0";
    }
    str += Num;

    int h = painter->fontMetrics().capHeight();
    if (h > m_rect.height())
    {
        OutInfo;
        qDebug() << "文字大小超过边框高度了，请调小一点";
        return;
    }
    w = painter->fontMetrics().horizontalAdvance(str);
    painter->drawText(QPoint(m_rect.x()+(m_rect.width() -w) / 2,m_rect.y()+h+(m_rect.height()-h)/2),str );
}

void Score::setPlayer(Sprite* player)
{
	m_player = player;
}

void Score::setNumTextPen(const QPen& pen)
{
    m_NumPen = pen;
}

void Score::setNumTextFont(const QFont& font)
{
    m_NumFont = font;
}

void Score::setRectPen(const QPen& pen)
{
    m_RectPen = pen;
}

